﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.Orders
{
	public partial class LetterOfAdvice : BasePage
	{
        private static ILetterOfAdviceBLL letterofAdviceBLL = IocContainer.Instance.Resolve<ILetterOfAdviceBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                this.txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtStartDate.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                //this.btnDelete.Enabled = false;
                //this.btnUpdate.Enabled = false;
                //this.btnSubmit.Enabled = false;
            }
        }


        [WebMethod]
        public static string GetLetterOfAdvice(string customer, string department, string sales, string code, string startDate, string endDate, string sidx, string sord, int page, int rows)
        {
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");

            if (!string.IsNullOrEmpty(customer))//客户
            {
                strWhere.Append(" and FCustid like '" + customer + "%'");
            }


            if (!string.IsNullOrEmpty(department))
            {
                strWhere.Append(" and FDept like '" + department + "%'");
            }
            if (!string.IsNullOrEmpty(sales))
            {
                strWhere.Append(" and Fsalse like '" + sales + "%'");
            }
            if (!string.IsNullOrEmpty(code))
            {
                strWhere.Append(" and Fnumber= '" + code + "'");
            }
            if (!string.IsNullOrEmpty(startDate) && string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and Fdate >='" + startDate + "'");
            }
            else if (string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and Fdate <='" + endDate.Trim() + " 23:59:59'");
            }
            else if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and Fdate between '" + startDate + "' and '" + endDate.Trim() + " 23:59:59'");
            }

            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(sidx) ? " " + sidx : "  Fnumber  ");
            strOrderBy.Append(!string.IsNullOrEmpty(sord) ? " " + sord : "  desc ");

            int totalRecords = 0;
            int totalPages = 0;
            DataTable dtProducts = letterofAdviceBLL.GetLetterOfAdviceByPage(strWhere.ToString(), strOrderBy.ToString(), page, rows, out totalRecords,
                                           out totalPages);

            string strResult = SerializationHelper.DataTable2Json(dtProducts);
            
            return "{\"records\":\"" + totalRecords.ToString() + "\" ,\"total\":\"" + totalPages.ToString() + "\",\"page\":\"" + page.ToString() + "\" ,\"rows\":" +
                   strResult + " }";

        }

        [WebMethod]
        public static string GetDetailedLetterOfAdvice(string id,  string sidx, string sord, int page, int rows)
        {
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");
            string strReturnValue = "";
            if (!string.IsNullOrEmpty(id))
            {
                strWhere.Append(" and FSaleStockID=" + id);
                StringBuilder strOrderBy = new StringBuilder(" Order by ");
                strOrderBy.Append(!string.IsNullOrEmpty(sidx) ? " " + sidx : "  FOrdernumber  ");
                strOrderBy.Append(!string.IsNullOrEmpty(sord) ? " " + sord : "  desc ");

                int totalRecords = 0;
                int totalPages = 0;
                DataTable dtProducts = letterofAdviceBLL.GetDetailedLetterOfAdviceByPage(strWhere.ToString(), strOrderBy.ToString(), page, rows, out totalRecords,
                                               out totalPages);

                string strResult = SerializationHelper.DataTable2Json(dtProducts);

                strReturnValue= "{\"records\":\"" + totalRecords.ToString() + "\" ,\"total\":\"" + totalPages.ToString() + "\",\"page\":\"" + page.ToString() + "\" ,\"rows\":" +
                       strResult + " }";
            }

            return strReturnValue;

        }

        [WebMethod]
        public static void Export2Excel(string customer, string department, string sales, string code, string startDate, string endDate, string sidx, string sord)
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();
            ////create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "NPOI Team";
            hssfworkbook.DocumentSummaryInformation = dsi;

            ////create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "NPOI SDK Example";
            hssfworkbook.SummaryInformation = si;

            ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
            sheet1.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample");
            int x = 1;
            for (int i = 1; i <= 15; i++)
            {
                IRow row = sheet1.CreateRow(i);
                for (int j = 0; j < 15; j++)
                {
                    row.CreateCell(j).SetCellValue(x++);
                }
            }

             MemoryStream file = new MemoryStream();
            hssfworkbook.Write(file);

             string filename="test.xls";
            HttpContext curContext = HttpContext.Current;
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
            curContext.Response.Clear();
            curContext.Response.BinaryWrite(file.GetBuffer());
            curContext.Response.End();

        }

	    protected void btnUpdate_Click(object sender, EventArgs e)
        {
           string str= letterofAdviceBLL.Export2Excel("");
        }
    }
}
